$(document).ready(init);

function init(){
	initData();
	renderPage();
	bindEvent();
	initErrorHandler();
}

function initData(){
	
}

function renderPage(){
	util.addSubmitToken($("#bookForm"),responseVO.objects['submitToken']);
	
}

function bindEvent(){
	$('#syncCreateBt').click(createSyncMode);
	$('#asynCreateBt').click(createAsynMode);
	$("#bookForm").validationEngine({validationEventTrigger:"checkFrom"});
}

function initErrorHandler(){
	$(document).bind('errCodeNeedLogin',popLoginDialog);
}


function createSyncMode(){
	$('#bookForm').trigger("checkFrom");
	var checkResult = $("#bookForm").validationEngine('validate');
	if (!checkResult) return;
	
	$("#bookForm").attr("action",webappName+"/book/createBook");
	$("#bookForm").submit();
	
}

function createAsynMode(){
	$('#bookForm').trigger("checkFrom");
	var checkResult = $("#bookForm").validationEngine('validate');
	if (!checkResult) return;
	
	var formData = $("#bookForm").serialize();
	
	$.post(webappName+"/book/createBook",formData,onCreateAsynCallback,"json");
	
}

function onCreateAsynCallback(asynResponseVO){
	responseVO = asynResponseVO;
	var result = responseVO.executeResult;
	if (result){
		alert('创建成功');
	}else{
		$(document).trigger(responseVO.errCode)
		//alert('创建失败('+responseVO.errCode+')');
	}
	util.updateSubmitToken($("#bookForm"),responseVO.objects['submitToken']);
	util.resetForm($("#bookForm"));
}

function popLoginDialog(){
	util.popLoginDialog();
}

